package com.kaohe.web;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 
			 try {
				//处理中文乱码的问题
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
					
				//获取方法
				String method = request.getParameter("method");

				// 1.获取字节码
				Class clazz = this.getClass();

				// 2.获取方法
				Method me = clazz.getMethod(method, HttpServletRequest.class,HttpServletResponse.class);
				
				// 3.执行
				String path = (String) me.invoke(this, request,response);
				
				if(path!=null&&!"".equals(path)){
					//抽取请求转发
					request.getRequestDispatcher(path).forward(request, response);
					
				}
				
				
			} catch (Exception e) {
 
				System.out.println("没有这个方法");
				e.printStackTrace();
			}
 
			 
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}